<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{artObj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="artObj.pubdate">
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="artObj.is_followed" @click="followedFn(false)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="followedFn(true)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content">好好学习, 天天向上</div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="artObj.attitude===1" @click="likeFn(false)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="likeFn(true)">点赞</van-button>
      </div>
    </div>
    <!-- 评论列表 -->
    <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
        :immediate-check="false"
      >
    <CommentList v-for="item in commentList" :key="item.com_id" :obj="item" :isShowCmtInput="isShowCmtInput"></CommentList>
    </van-list>
      <!-- 底部添加评论区域 - 1 -->
<div class="add-cmt-box van-hairline--top" v-if="isShowCmtInput === true">
    <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
    <div class="ipt-cmt-div" @click="isShowCmtInput = false">发表评论</div>
    <div class="icon-box">
        <van-badge :content="totalCount" max="99">
            <van-icon name="comment-o" size="0.53333334rem" @click="moveFn"/>
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
    </div>
</div>

<!-- 底部添加评论区域 - 2 -->
<div class="cmt-box van-hairline--top" v-else>
    <textarea v-focus placeholder="友善评论、理性发言、阳光心灵" @blur="blurFn" v-model="comValue"></textarea>
    <van-button type="default" :disabled="comValue.length===0" @click="sendFn">发布</van-button>
</div>
  </div>
</template>

<script>
import { articleDetailAPI, followedUserAPI, unFollowedUserAPI, articleLikeAPI, articleDisLikeAPI, commentListAPI, sendCommentAPI } from '@/api'
import CommentList from './CommentList.vue'
export default {
  data () {
    return {
      artObj: {},
      commentList: [],
      totalCount: 0,
      isShowCmtInput: true,
      comValue: '',
      loading: false,
      finished: false,
      offset: null
    }
  },
  components: {
    CommentList
  },
  async created () {
    // console.log(this.$route.query.aid)
    // console.log(articleDetailAPI())
    const res = await articleDetailAPI({ id: this.$route.query.aid })
    // console.log(res)
    this.artObj = res.data.data
    this.getCommentListFn()
  },
  methods: {
    async getCommentListFn () {
      const res1 = await commentListAPI({ artId: this.$route.query.aid, offset: this.offset })
      if (res1.data.data.results.length === 0) {
        this.finished = true
      }
      // console.log(res1)
      this.commentList = [...this.commentList, ...res1.data.data.results]
      this.totalCount = res1.data.data.total_count || ''
      this.offset = res1.data.data.last_id
      this.loading = false
    },
    onLoad () {
      this.getCommentListFn()
    },
    async blurFn () {
      setTimeout(() => {
        this.isShowCmtInput = true
      }, 1000)
    },
    async sendFn () {
      const res = await sendCommentAPI({
        artId: this.$route.query.aid, content: this.comValue
      })
      // console.log(res)
      this.commentList.unshift(res.data.data.new_obj)
      this.totalCount++
      this.comValue = ''
    },
    async moveFn () {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    },
    async followedFn (bool) {
      if (bool === true) {
        this.artObj.is_followed = true
        await followedUserAPI({
          target: this.artObj.aut_id
        })
      } else {
        this.artObj.is_followed = false
        await unFollowedUserAPI({
          uid: this.artObj.aut_id
        })
      }
    },
    async likeFn (bool) {
      if (bool === true) {
        this.artObj.attitude = 1
        await articleLikeAPI({
          target: this.$route.query.aid
        })
      } else {
        this.artObj.attitude = -1
        await articleDisLikeAPI({
          artId: this.$route.query.aid
        })
      }
    }
  }

}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
width: 100%;
}
/deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
